//
//  KLApplyToBeChannelController.m
//  KunLun
//
//  Created by Mac on 2017/12/25.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLApplyToBeChannelController.h"
#import "KLChannelViewController.h"
#import "STTextView.h"
#define XInsetValeu 15
@interface KLApplyToBeChannelController ()
@property(nonatomic, strong) UITextField                     *realNameTextfiled;
@property(nonatomic, strong) UITextField                     *phoneTextfiled;
@property(nonatomic, strong) STButton                     *addressButton;
@property(nonatomic, strong) UITextField                     *addressTextFiled;
@property(nonatomic, strong) NSString                     *codeString;

@property(nonatomic, strong) STButton                     *cardButton;
@property(nonatomic, strong) STButton                     *cardBackButton;
@property(nonatomic, strong) UIImage                      *originmage ;

@property(nonatomic, strong) STTextView                     *destextView;

@property(nonatomic, strong) NSString                     *cardImageUrl;
@property(nonatomic, strong) NSString                     *cardbackImageUrl;

@end

@implementation KLApplyToBeChannelController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"渠道申请";
     self.originmage = [UIImage imageNamed:@"sz_xiangce"];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView st_autoAdjustAllResponder];
    [self configSubView];
    [self configFooterView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    header.backgroundColor = [UIColor whiteColor];
    
    //真实姓名
    UIView * realNameView = [self headerViewWithTitle:@"真实姓名:"];
    realNameView.top = 0;
    self.realNameTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, realNameView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.realNameTextfiled.placeholder = @"请输入真实姓名";
    self.realNameTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [header addSubview:realNameView];
    [header addSubview:self.realNameTextfiled];
    UIView * line3 = [[UIView alloc] initWithFrame:CGRectMake(0, self.realNameTextfiled.bottom, UIScreenWidth, 0.5)];
    line3.backgroundColor = KL_lineColor;
    [header addSubview:line3];
    

    UIView * idView = [self headerViewWithTitle:@"联系方式:"];
    idView.top = line3.bottom;
    self.phoneTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, idView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.phoneTextfiled.placeholder = @"请输入联系方式";
    self.phoneTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneTextfiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:idView];
    [header addSubview:self.phoneTextfiled];
    UIView * line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.phoneTextfiled.bottom, UIScreenWidth, 0.5)];
    line2.backgroundColor = KL_lineColor;
    [header addSubview:line2];

    __weak typeof(self) weakSelf =  self;
    //经营地址
    self.addressButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, line2.bottom, UIScreenWidth - 2*XInsetValeu, 40)
                                                   title:@"点击修改经营地址"
                                              titleColor:KL_firstTextColor
                                               titleFont:15
                                            cornerRadius:0
                                         backgroundColor:nil
                                         backgroundImage:nil
                                                   image:nil];
    [self.addressButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddressButton];
    }];
    [header addSubview:self.addressButton];
    
    
    UIView * addtextView = [self headerViewWithTitle:@"详细地址:"];
    addtextView.top = self.addressButton.bottom;
    self.addressTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, addtextView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.addressTextFiled.placeholder = @"请输入联系方式";
    self.addressTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [header addSubview:addtextView];
    [header addSubview:self.addressTextFiled];
    
    UIView * line5 = [[UIView alloc] initWithFrame:CGRectMake(0, self.addressTextFiled.bottom, UIScreenWidth, 0.5)];
    line5.backgroundColor = KL_lineColor;
    [header addSubview:line5];
    
    //证件照
    UIView * cardView = [self headerViewWithTitle:@"上传证件照:"];
    cardView.top = line5.bottom + 20;
    [header addSubview:cardView];
    
    self.cardButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, cardView.bottom,100 , 80)
                                                    title:@""
                                               titleColor:KL_thirdTextColor
                                                titleFont:14
                                             cornerRadius:0
                                          backgroundColor:KL_backgroundColor
                                          backgroundImage:nil
                                                    image:_originmage];
    self.cardButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cardButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCardButton];
    }];
    [self.cardButton setTitleEdgeInsets:UIEdgeInsetsMake(20, 0, 0, 0)];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, self.cardButton.width, 20)
                                                     text:@"请上传证件照正面"
                                                textColor:KL_secendTextColor
                                                     font:10
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.bottom = self.cardButton.height - 5;
    titleLable.tag =10000;
    [self.cardButton addSubview:titleLable];
    [header addSubview:self.cardButton];
    
    self.cardBackButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu +self.cardButton.right , cardView.bottom,100 , 80)
                                                title:@""
                                           titleColor:KL_thirdTextColor
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:KL_backgroundColor
                                      backgroundImage:nil
                                                image:_originmage];
    self.cardBackButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cardBackButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedBackCardButton];
    }];
    [self.cardBackButton setTitleEdgeInsets:UIEdgeInsetsMake(20, 0, 0, 0)];
    STLabel * titleLable1 = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, self.cardBackButton.width, 20)
                                                     text:@"请上传证件照背面"
                                                textColor:KL_secendTextColor
                                                     font:10
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable1.bottom = self.cardBackButton.height - 5;
     titleLable1.tag =10000;
    [self.cardBackButton addSubview:titleLable1];
    [header addSubview:self.cardBackButton];
    
    
    //点铺简介
    UIView * desView = [self headerViewWithTitle:@"个人简介:"];
    desView.top = self.cardBackButton.bottom  +30;
    [header addSubview:desView];
    self.destextView = [[STTextView alloc] initWithFrame:CGRectMake(XInsetValeu, desView.bottom, UIScreenWidth - 2*XInsetValeu, 60)];
    self.destextView.placeholder = @"请输入店铺简介";
    [self.destextView st_setBorderWith:1 borderColor:KL_lineColor cornerRadius:5];
    [header addSubview:self.destextView];
    
    header.height = self.destextView.bottom;
    self.tableView.tableHeaderView = header;

}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = [UIColor whiteColor];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30,  40, UIScreenWidth - 60, 44)
                                                        title:@"立即申请"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:KL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    
    footer.height = confimButton.bottom + 20;
    self.tableView.tableFooterView = footer;
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    backView.backgroundColor = [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, 0, UIScreenWidth - 2* XInsetValeu, 44)
                                                     text:title
                                                textColor:KL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [backView addSubview:titleLable];
    return backView;
}
#pragma mark --Action Method
- (void)onSelectedAddressButton{
    
    KLChannelViewController * vc = [KLChannelViewController new];
    [vc setDidChoseArea:^(NSString *places, NSString *codes) {
        [self.addressButton setTitle:places forState:UIControlStateNormal];
        self.codeString = codes;
    }];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onSelectedCardButton{
    __weak typeof(self) weakSelf =  self;
    [self showDefultImagePicker:^(UIImage *image) {
        [weakSelf.cardButton setImage:image forState:UIControlStateNormal];
        UIView * view = [self.cardButton viewWithTag:10000];
        [view removeFromSuperview];
        [KLUtils sendQiNiuWithImage:self.cardButton.currentImage handle:^(NSString *imageUrl, NSString *errorDes) {
            self.cardImageUrl = imageUrl;
        }];
    }];
}
- (void)onSelectedBackCardButton{
    __weak typeof(self) weakSelf =  self;
    [self showDefultImagePicker:^(UIImage *image) {
        [weakSelf.cardBackButton setImage:image forState:UIControlStateNormal];
        UIView * view = [self.cardBackButton viewWithTag:10000];
        [view removeFromSuperview];
        [KLUtils sendQiNiuWithImage:self.cardBackButton.currentImage handle:^(NSString *imageUrl, NSString *errorDes) {
            self.cardbackImageUrl = imageUrl;
        }];
    }];
}
- (void)onSelectedCommitButton{

    if (!self.realNameTextfiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入真实姓名"];
        return;
    }
    if (!self.phoneTextfiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入联系方式"];
        return;
    }
    if ([self.addressButton.currentTitle isEqualToString:@"点击修改经营地址"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择经营地址"];
        return;
    }
    if (!self.addressTextFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入详细地址"];
        return;
    }
    if (self.cardButton.currentImage == self.originmage) {
        [SVProgressHUD showErrorWithStatus:@"请选择证件正面照片"];
        return;
    }
    if (self.cardBackButton.currentImage == self.originmage) {
        [SVProgressHUD showErrorWithStatus:@"请选择证件背面照片"];
        return;
    }
    if (!self.destextView.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入个人简介"];
        return;
    }
    if (!self.cardImageUrl) {
        [KLUtils sendQiNiuWithImage:self.cardButton.currentImage handle:^(NSString *imageUrl, NSString *errorDes) {
            self.cardImageUrl = imageUrl;
        }];
        [SVProgressHUD showErrorWithStatus:@"正在上传证件照正面，请稍后"];
        return;
    }
    if (!self.cardbackImageUrl) {
        [KLUtils sendQiNiuWithImage:self.cardBackButton.currentImage handle:^(NSString *imageUrl, NSString *errorDes) {
            self.cardbackImageUrl = imageUrl;
        }];
        [SVProgressHUD showErrorWithStatus:@"正在上传证件照背面，请稍后"];
        return;
    }
    [self sendTobeChannelRequest];
}
#pragma mark --NetWork Method
- (void)sendTobeChannelRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/applychannel",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    
    if (self.realNameTextfiled.text.length) {
        [dic setObject:self.realNameTextfiled.text forKey:@"name"];
    }
    if (self.phoneTextfiled.text.length) {
        [dic setObject:self.phoneTextfiled.text forKey:@"phone"];
    }
    if (self.codeString) {
        NSArray * array = [self.codeString componentsSeparatedByString:@"-"];
        
        NSString * province = array.firstObject;
        [dic setObject:province forKey:@"province"];
        NSString * city = array[1];
        [dic setObject:city forKey:@"city"];
        if (array.count > 2) {
            NSString * county = array[2];
            [dic setObject:county forKey:@"county"];
        }
        if (array.count > 3) {
            NSString * town = array[3];
            [dic setObject:town forKey:@"town"];
        }
        
    }
    if (self.addressTextFiled.text.length) {
        [dic setObject:self.addressTextFiled.text forKey:@"address"];
    }
    if (self.destextView.text.length) {
        [dic setObject:self.destextView.text forKey:@"abstract"];
    }
    if (self.cardImageUrl.length) {
        [dic setObject:self.cardImageUrl forKey:@"ID_photo1"];
    }
    if (self.cardbackImageUrl.length) {
        [dic setObject:self.cardbackImageUrl forKey:@"ID_photo2"];
    }

    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"申请成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
